package xyz.yudan.app.service;

import xyz.yudan.app.dao.base.BaseTask;
import xyz.yudan.app.dao.base.BaseTeam;
import xyz.yudan.app.dao.base.BaseUser;
import xyz.yudan.app.dao.biz.BizTeam;
import xyz.yudan.app.index.UserTeamIndex;
import xyz.yudan.app.relation.UserTeamRelation;
import xyz.yudan.app.util.YDUtil;

import java.util.ArrayList;

/**
 * Created by gamy on 2018/4/6.
 */
final public class TeamService extends BaseService {

    //创建团队
    public static BizTeam createTeam(String userId, String teamName) {
        BaseTeam team = new BaseTeam();
        team.setTeamId(YDUtil.getUUID());
        team.setCreateTime(YDUtil.getTimestamp());
        team.setCreatorUid(userId);
        team.setTeamName(teamName);

        boolean flag = saveTeam(team);
        if (flag) {
            return convertBaseTeam(userId, team);
        } else {
            return null;
        }
    }


    //更新团队
    public static boolean updateTeam(String userId, String teamId, String teamName, String logoUrl, String description) {
        BaseTeam team = getBaseTeam(teamId);

        if (team == null) {
            return false;
        }

        if (YDUtil.stringIsNotEmpty(teamName)) {
            team.setTeamName(teamName);
        }

        if (YDUtil.stringIsNotEmpty(logoUrl)) {
            team.setLogoUrl(logoUrl);
        }

        if (YDUtil.stringIsNotEmpty(description)) {
            team.setDescription(description);
        }

        return saveTeam(team);
    }

    //申请加入团队
    public static boolean applyToJoinTeam(String userId, String teamId) {
        UserTeamIndex index = UserTeamIndex.getIndex(userId, teamId);
        if (index == null) {
            index = UserTeamIndex.createIndex(userId, teamId);
        }
        if (index.getRole() == UserTeamIndex.TeamRole.NONE) {
            index.setRole(UserTeamIndex.TeamRole.APPLICANT);
            return index.save();
        }
        return false;
    }

    //获取申请列表
    public static ArrayList<BaseUser> getTeamApplicants(String userId, String teamId) {
        int role = UserTeamIndex.getRole(userId, teamId);
        if (role != UserTeamIndex.TeamRole.CREATOR && role != UserTeamIndex.TeamRole.MANAGER) {
            //没有管理权限的返回空
            return null;
        }
        return null;
    }

    //同意加入申请
    public static boolean acceptTeamApplicant(String userId, String teamId, String applicant) {
        int role = UserTeamIndex.getRole(userId, teamId);
        if (role != UserTeamIndex.TeamRole.CREATOR && role != UserTeamIndex.TeamRole.MANAGER) {
            return false;
        }
        UserTeamIndex index = UserTeamIndex.getIndex(applicant, teamId);
        if (index != null && index.getRole() == UserTeamIndex.TeamRole.APPLICANT) {
            index.setRole(UserTeamIndex.TeamRole.MEMBER);
            return index.save();
        }
        return false;
    }

    //拒绝加入申请
    public static boolean rejectTeamApplicant(String userId, String teamId, String applicant) {
        int role = UserTeamIndex.getRole(userId, teamId);
        if (role != UserTeamIndex.TeamRole.CREATOR && role != UserTeamIndex.TeamRole.MANAGER) {
            return false;
        }
        UserTeamIndex index = UserTeamIndex.getIndex(applicant, teamId);
        if (index != null && index.getRole() == UserTeamIndex.TeamRole.APPLICANT) {
            index.setRole(UserTeamIndex.TeamRole.NONE);
            return index.save();
        }
        return false;
    }

    //修改成员的角色
    public static boolean changeTeammateRole(String userId, String teamId, String teammate, int newRole) {
        int role = UserTeamIndex.getRole(userId, teamId);
        boolean isCreator = (role == UserTeamIndex.TeamRole.CREATOR);
        boolean isManager = (role == UserTeamIndex.TeamRole.MANAGER);
        boolean isCreatorOrManager = isCreator || isManager;

        if (!isCreatorOrManager) {
            //非管理员以上权限不能修改角色
            return false;
        }

        boolean newRoleValid = (UserTeamIndex.TeamRole.MEMBER == newRole
                || UserTeamIndex.TeamRole.MANAGER == newRole
                || UserTeamIndex.TeamRole.NONE == newRole);

        if (!newRoleValid) {
            return false;
        }

        UserTeamIndex index = UserTeamIndex.getIndex(teammate, teamId);
        if (index == null) {
            return false;
        }
        int oldRole = index.getRole();

        boolean oldRoleValid = (UserTeamIndex.TeamRole.MEMBER == newRole
                || UserTeamIndex.TeamRole.MANAGER == newRole
                || UserTeamIndex.TeamRole.NONE == newRole);

        if (!oldRoleValid) {
            return false;
        }

        if ((oldRole == UserTeamIndex.TeamRole.MANAGER || newRole == UserTeamIndex.TeamRole.MANAGER) && !isCreator) {
            //涉及管理员变动,必须创始人才能改
            return false;
        }

        if (role != UserTeamIndex.TeamRole.CREATOR) {
            return false;
        }

        index.setRole(newRole);
        return index.save();
    }

    public static boolean removeTeammate(String userId, String teamId, String teammate) {
        return changeTeammateRole(userId, teamId, teammate, UserTeamIndex.TeamRole.NONE); //变成没有权限就是删除了
    }

    //获取团队信息
    public static ArrayList<BizTeam> getUserTeamList(String userId) {
        return null;
    }

    //获取团队信息
    public static BizTeam getBizTeam(String userId, String teamId) {
        BaseTeam baseTeam = getBaseTeam(teamId);
        BizTeam bizTeam = convertBaseTeam(userId, baseTeam);
        return bizTeam;
    }

    //获取团队基本信息
    public static BaseTeam getBaseTeam(String teamId) {
        return null;
    }

    private static boolean saveTeam(BaseTeam team) {
        return true;
    }

    public static BizTeam convertBaseTeam(String userId, BaseTeam baseTeam) {
        BizTeam team = new BizTeam(baseTeam);
        // file the creator
        team.setCreator(UserService.getBaseUser(userId));
        // fill the managers
        team.setManagers(UserService.getBaseUsers(team.getManagerUidList()));
        // fill the members
        team.setMembers(UserService.getBaseUsers(team.getMemberUidList()));
        // fill the relation
        team.setRelation(BizTeam.calRelation(team, userId));

        return team;
    }
}
